package com.jiankangjin.weixinchannel.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.weixinchannel.services.INotifyService;
import com.jiankangjin.weixinchannel.util.ResponseHandler;

/**
 * 【微信支付】公众号支付接口文档V2.6
 * 接收微信 通知 接口
 * 间隔时间为：8s、10s、10s、30s、30s、60s、120s、360s、1000s。
 */
@Controller
public class NotifyController {
	private static final Log logger=LogFactory.getLog(NotifyController.class);
	
	@Autowired
	private INotifyService notifyService;
	
	/**
	 * 接收微信后台通知接口
	 */
	@RequestMapping(value="/notify")
	@ResponseBody
	public String notify(HttpServletRequest request, HttpServletResponse response) {
		try {
			ResponseHandler resHandler = new ResponseHandler(request, response);
			
			logger.info("键值参数:"+resHandler.paramsMap);
			logger.info("XML参数:"+resHandler.xmlMap);
			
			String openId = resHandler.xmlMap.get("OpenId");//付款人微信号
			
			String partner = resHandler.paramsMap.get("partner");
			String out_trade_no = resHandler.paramsMap.get("out_trade_no");
			String transaction_id = resHandler.paramsMap.get("transaction_id");
			String total_fee = resHandler.paramsMap.get("total_fee");
			String time_end = resHandler.paramsMap.get("time_end");
			String discount = resHandler.paramsMap.get("discount");
			String trade_state = resHandler.paramsMap.get("trade_state");
			logger.info("商户:"+ partner +",订单号:" + out_trade_no + ",财付通交易号:"
						+ transaction_id + ",金额:" + total_fee + ",折扣值:"
						+ discount + ",支付状态0为成功:" + trade_state+",交易完成时间:"+time_end
						+ ",付款人OpenId:" + openId);
			if (resHandler.isValidSign()) {
				logger.info("财付通签名MD5通过 ");
				if (resHandler.isWXsign()) {
					logger.info("微信签名SHA1验证成功");
					if ("0".equals(trade_state)) {
						logger.info("支付成功,处理中...");
						if (notifyService.doPayed(out_trade_no, transaction_id, total_fee, time_end, trade_state)) {
							/*/发货
							if (notifyService.doDeliverNotify(openId, transaction_id, out_trade_no)) {
								return "success";
							}*/
							//作未发货处理
							logger.info("不发货");
						}
					} else {
						logger.info("支付失败,无处理...");
					}
				} else {
					logger.error("微信签名SHA1验证失败");
				}
			} else {
				logger.error("财付通签名MD5验证失败");
			}
		} catch (Exception e) {
			logger.error(this, e);
		}
		return "fail";
	}
}

/* 通知实例:
 * 
 * 键值参数:{
 * bank_billno=201408313080670569, 
 * bank_type=3106, 
 * discount=0, 
 * fee_type=1, 
 * input_charset=GBK, 
 * notify_id=78x2qxrcgxYXyn4uC0k7LPAQ063OAzfAJzq8mxELvOV7xxi3_p24w6oW1zZLLw0B5ZfIg8u-9jGwliheVroUoc17pFSH9f7k, 
 * out_trade_no=20140831112107661-000018x3Pdvvuv, 
 * partner=1220586901, 
 * product_fee=1, 
 * sign=A1096E06228C9EDD98A92F6CC02CC37D, 
 * sign_type=MD5, 
 * time_end=20140831112118, 
 * total_fee=1, 
 * trade_mode=1, 
 * trade_state=0, 
 * transaction_id=1220586901201408313225814404, 
 * transport_fee=0}
 * 
 * XML参数:{
 * AppId=wxcec8863ae602c67c, 
 * AppSignature=448f276acdea9c59f5eeee36edb2b75303f6784e, 
 * IsSubscribe=1, 
 * NonceStr=iNwya9uMMEdqgmiH, 
 * OpenId=obDekuBWSsFOzgcXqwRamlEA26BI, 
 * SignMethod=sha1, 
 * TimeStamp=1409455278}
 */
